<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.name" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="query.state" clearable placeholder="请选择">
            <el-option label="禁用" value="0"></el-option>
            <el-option label="启用" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="initPage">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="emps" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="username" label="姓名" width="120" sortable>
      </el-table-column>
      <el-table-column prop="department.name" label="部门名称" min-width="120" sortable>
      </el-table-column>
      <el-table-column prop="age" label="年龄" min-width="120" sortable>
      </el-table-column>
      <el-table-column prop="state" label="是否启用" width="100" >
        <template slot-scope="scope">
            <div slot="reference" >
              <el-tag size="medium" v-if="scope.row.state==0" type="danger">禁用</el-tag>
              <el-tag size="medium" v-if="scope.row.state==1" type="success">启用</el-tag>
            </div>
        </template>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="100" sortable>
      </el-table-column>
      <el-table-column prop="phone" label="电话" width="120" sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.currentPage"
          :page-sizes="[5, 10, 25, 50]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="saveForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="department_id">
          <template>
            <el-select v-model="saveForm.department_id" clearable placeholder="请选择">
              <el-option
                  v-for="item in department"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="是否启用">
          <el-radio-group v-model="saveForm.state">
            <el-radio class="radio" :label="1">启用</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="邮箱" prop="eamil">
          <el-input v-model="saveForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="saveForm.phone" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>


export default {
  data() {
    return {
      query: {
        username: '',
        state:''
      },
      emps: [],//表格数据
      total: 0,//总条数
      /*currentPage: 1,//当前页
      pageSize:10,//每页显示条数*/
      listLoading: false,//
      sels: [],//列表选中列

      saveFormVisible: false,//编辑界面是否显示
      saveLoading: false,
      title:'',
      saveFormRules: {//表单验证
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
        ],
        age: [
          { required: true, message: '请输入年龄', trigger: 'blur' },
        ],
        phone: [
          { required: true, message: '请输入电话', trigger: 'blur' },
        ],
        department_id:[
          { required: true, message: '请选择部门', trigger: 'blur' },
        ]
      },
      department:{},
      //编辑界面数据
      saveForm: {
        id: null,
        username: '',
        department_id:null,
        state: 0,
        email: '',
        phone: ''
      },
      queryInfo:{
        username:'',
        state:null,
        currentPage:1,
        pageSize:10
      }
    }
  },
  methods: {
    //当前页改变
    handleCurrentChange(val) {
      this.queryInfo.currentPage = val;
      this.getEmps();
    },
    //每页显示条数改变
    handleSizeChange(val){
      this.queryInfo.pageSize=val;
      this.getEmps()
    },
    //查询和删除时初始化queryInfo
    initPage(){
      this.queryInfo.currentPage=1
      this.queryInfo.username=this.query.username
      this.queryInfo.state=this.query.state
      this.getEmps()
    },
    //新增和修改时初始化queryInfo
    savePage(){
      if (!this.saveForm.id){
        this.queryInfo.currentPage=1
        this.queryInfo.username=''
        this.queryInfo.state=''
      }
      this.getEmps()
    },

    //获取用户列表
    getEmps() {
      /*let para = {
        pageSize: this.pageSize,
        currentPage:this.currentPage,
        username: this.query.username,
        state: this.query.state
      };*/
      console.log(1)
      this.listLoading = true;
      this.$http.post("/emp/listPage",this.queryInfo).then(res=>{
        if (res.data.flag){
          this.emps=res.data.data
          this.total=res.data.total
        }
      })
      this.listLoading = false;

      //NProgress.start();
      /*getUserListPage(para).then((res) => {
        this.total = res.data.total;
        this.users = res.data.users;
        this.listLoading = false;
        //NProgress.done();
      });*/
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        //开启忙等
        this.listLoading = true;
        this.$http.delete("/emp/"+row.id).then(res=>{
          console.log(res)
          if (res.data.flag){
            this.$message({
              message: res.data.msg,
              type: 'success'
            });
            this.initPage()
          }else {
            this.$message({
              message: res.data.msg,
              type: 'warning'
            });
          }
        })
        //关闭忙等
        this.listLoading=false

      })
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      if (this.$refs['saveForm']){
        this.$refs['saveForm'].resetFields();
      }
      this.title='编辑'
      this.saveFormVisible = true;
      this.saveForm = Object.assign({}, row);
    },
    //显示新增界面
    handleAdd: function () {
      this.title="新增"
      if (this.$refs['saveForm']){
        this.$refs['saveForm'].resetFields();
      }
      this.saveFormVisible = true;
      //清空表单验证
      this.saveForm = {
        id: null,
        username: '',
        department_id: null,
        state: 0,
        age: '',
        email: '',
        phone: ''
      };
    },
    //获取所有部门数据
    getDepts(){
      this.$http.get("/dept/list").then(res=>{
        this.department=res.data.data
      })
    },
    //数据保存
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            //开启忙等
            this.saveLoading = true;
            console.log(this.saveForm)
            //发送请求
            this.$http.put("/emp",this.saveForm).then(res=>{
              if (res.data.flag){
                this.$message({
                  message: res.data.msg,
                  type: 'success'
                });
              }else {
                this.$message({
                  message: res.data.msg,
                  type: 'warning'
                });
              }
              console.log(3)
              this.savePage()
              this.saveLoading = false;
              this.saveFormVisible = false;
              this.$refs['saveForm'].resetFields();

          })
        })
        }})
    },

    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id).toString();
      console.log(ids)
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        //开启忙等
        this.listLoading = true;
        var ids=this.sels.map(item=>item.id)
        this.$http.patch("/emp/del",ids).then(res=>{
          if (res.data.flag){
            this.$message({
              message:res.data.msg,
              type:'success'
            })
            this.initPage();
          }else {
            this.$message({
              message:res.data.msg,
              type:'warning'
            })
          }
        })
        this.listLoading = true;
        //NProgress.start();
        /*batchRemoveUser(para).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getEmps();
        });*/
      });
    }
  },
  mounted() {
    this.getEmps();
    this.getDepts()
  }
}

</script>

<style scoped>

</style>